package com.soft.servlet;

import com.soft.entity.Goods;
import com.soft.entity.GoodsType;
import com.soft.service.Domain;
import com.soft.service.MISC;
import com.soft.util.PropUtil;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

/**
 * @author kongz
 * @version 1.0 2021/6/1 16:35
 * @description 商品添加servlet
 */
@WebServlet(name = "GoodsAddServlet", urlPatterns = "/GoodsAdd")
public class GoodsAddServlet extends HttpServlet {
    private Domain domain;
    private MISC misc;
    private boolean isMultipart;//判断是否有文件上传
    private FileItem fileItem;//暂时存储文件
    private int maxFileSize = 1024 * 1024 * 10;
    private int maxMemSize = 100 * 1024;

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        domain = new Domain();
        List<GoodsType> list = domain.getGoodsTypeAll();

        req.setAttribute("GoodsType", list);
        req.getRequestDispatcher("toGoodsManageAdd.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        domain = new Domain();
        misc = new MISC();

        String name = null;
        String price = null;
        String date = misc.getCurrentDate();
        String count = null;
        String type = null;
        String detail = null;
        Goods goods = new Goods();

        isMultipart = ServletFileUpload.isMultipartContent(req);

        if (!isMultipart) {

            return;
        }

        DiskFileItemFactory factory = new DiskFileItemFactory();
        factory.setSizeThreshold(maxMemSize);

        String path = PropUtil.getProperty("img.path1");
        factory.setRepository(new File(path));

        // 创建一个新的文件上传处理程序
        ServletFileUpload upload = new ServletFileUpload(factory);
        upload.setSizeMax(maxFileSize);

        try {
            // 解析请求，获取文件项
            List fileItems = upload.parseRequest(req);
            // 处理上传的文件项
            Iterator i = fileItems.iterator();
            while (i.hasNext()) {
                FileItem fi = (FileItem) i.next();
                if (!fi.isFormField()) {
                    fileItem = fi;
                } else {
                    String value = fi.getString("utf-8");
                    if (fi.getFieldName().equals("shopGoods.goodsname")) name = value;
                    else if (fi.getFieldName().equals("shopGoods.price")) price = value;
                    else if (fi.getFieldName().equals("shopGoods.count")) count = value;
                    else if (fi.getFieldName().equals("shopGoods.type")) type= value;
                    else if (fi.getFieldName().equals("shopGoods.introduce")) detail = value;
                }
            }
            goods.setName(name);
            goods.setPrice(price);
            goods.setCreateTime(date);
            goods.setCount(count);
            goods.setType(type);
            goods.setDetail(detail);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (domain.insertGoods(goods))
            req.getSession().setAttribute("flag", 30);
        else
            req.getSession().setAttribute("flag", 31);

        List<Goods> goodsList = domain.getGoodsAll();
        int goodsId = goodsList.size();
        goodsId = goodsList.get(goodsId - 1).getId();
        // 写入文件
        File file = new File(path + "goods" + goodsId + ".jpg");
        try {
            fileItem.write(file);
        } catch (Exception e) {
            e.printStackTrace();
        }
        req.getRequestDispatcher("/GoodsList").forward(req, resp);
    }
}
